package com.xiannimei.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiannimei.model.Binding;
import com.xiannimei.model.BindingExample;
import com.xiannimei.model.Member;
import com.xiannimei.model.MemberExample;
import com.xiannimei.model.MemberExample.Criteria;
import com.xiannimei.model.command.LoginCmd;
import com.xiannimei.model.command.RegisterCmd;
import com.xiannimei.model.mapper.BindingMapper;
import com.xiannimei.model.mapper.MemberMapper;
import com.xiannimei.service.MemberService;
import com.xiannimei.service.oauth.OAuthProfile;
import com.xiannimei.util.EncryptUtil;
import com.xiannimei.web.MemberProfile;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private BindingMapper bindingMapper;

	@Override
	public Member getMember(String name) {
		Member res = null;
		MemberExample example = new MemberExample();
		example.createCriteria().andNickNameEqualTo(name);
		List<Member> users = memberMapper.selectByExample(example);
		if (users.size() > 0) {
			res = users.get(0);
		}
		return res;
	}

	@Override
	public int createMember(Member member) {
		return memberMapper.insert(member);
	}

	@Override
	public int createBinding(Binding member) {
		return bindingMapper.insert(member);
	}

	@Override
	public MemberProfile localLoginMember(LoginCmd loginCmd) {
		MemberExample exmp = new MemberExample();
		Criteria criteria = exmp.createCriteria();
		criteria.andLoginNameEqualTo(loginCmd.getLoginName());
		criteria.andMemPwdEqualTo(loginCmd.getMemPwd());
		List<Member> members = memberMapper.selectByExample(exmp);
		if (CollectionUtils.isNotEmpty(members)) {
			return new MemberProfile(members.get(0));
		} else
			return null;
	}

	@Override
	public MemberProfile registerMember(RegisterCmd registerCmd) {
		registerCmd.setCreateTime(new Date());
		registerCmd.setMemPwd(EncryptUtil.MD5Encode(registerCmd.getMemPwd()));
		createMember(registerCmd);
		return new MemberProfile(registerCmd);
	}

	@Override
	public Member findThirdPartyUID(String openId, Integer platFormId) {
		BindingExample eamp = new BindingExample();
		com.xiannimei.model.BindingExample.Criteria criteria = eamp.createCriteria();
		criteria.andPlatformIdEqualTo(platFormId);
		criteria.andPlatformUserCodeEqualTo(openId);
		List<Binding> bindings = bindingMapper.selectByExample(eamp);
		if (CollectionUtils.isNotEmpty(bindings)) {
			Integer memberId = bindings.get(0).getMemberId();
			return memberMapper.selectByPrimaryKey(memberId);
		}
		return null;
	}

	public void thirdPartyRegister(MemberProfile memberProfile) {

	}

	@Override
	public MemberProfile register(OAuthProfile profile, Member member) {
		member.setNickName(profile.getNick());
		member.setCreateTime(new Date());
		member.setIsOpenStat(0);
		member.setIsOpenBinding(0);
		member.setMemberStatus(Member.STATUS_FEDERATE_LOGIN);

		int memberId = createMember(member);

		Binding binding = new Binding();
		binding.setPlatformUserCode(profile.getUid());
		binding.setPlatformUserToken(profile.getToken());
		binding.setPlatformId(profile.getPlatformId());
		binding.setCreateTime(new Date());
		binding.setExpiredTime(new Date(new Date().getTime() + (profile.getRemain() * 1000L)));
		binding.setMemberId(memberId);

		createBinding(binding);
		return new MemberProfile(member);
	}

}
